//
//  SHHP_YHZX_LoginView.m
//  EpointMobileFrame
//
//  Created by 李林 on 13-3-29.
//  Copyright (c) 2013年 epoint. All rights reserved.
//

#import "MDUtil.h"
#import <QuartzCore/QuartzCore.h>
#import "WMH_Login_ViewController.h"
#import "WMH_User_Register_ViewController.h"
#import "Task_LoginCheck.h"

#define loginTaskId 1

@interface WMH_Login_ViewController ()

@end

@implementation WMH_Login_ViewController
{
    UIColor *darkcolor;
    UIColor *lightcolor;
    UIColor *darkcolor_light;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        //获取当前屏幕的高度
        CGRect rect_screen = [[UIScreen mainScreen]bounds];
        CGSize size_screen = rect_screen.size;
        int screenHeight = size_screen.height;
        if(screenHeight>=568){
            self = [super initWithNibName:@"WMH_Login_ViewController5" bundle:nibBundleOrNil];
        }else{
            self = [super initWithNibName:@"WMH_Login_ViewController" bundle:nibBundleOrNil];
        }
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    pagetype = [initParam objectForKey:@"pagetype"];//页面类型
    
    [self setTitle:@"用户登录"];
    
    
    darkcolor = [[UIColor alloc]
                 
                 initWithRed:(float)245.0/256.0 green:(float)130.0/256.0 blue:(float)32.0/256.0 alpha:(float)1.0];
    darkcolor_light = [[UIColor alloc]
                       
                       initWithRed:(float)245.0/256.0 green:(float)130.0/256.0 blue:(float)32.0/256.0 alpha:(float)0.8];
    
    lightcolor = [[UIColor alloc]
                  
                  initWithRed:(float)244.0/256.0 green:(float)244.0/256.0 blue:(float)244.0/256.0 alpha:(float)1.0];
    [self initLoginview];
    [self.view setBackgroundColor:[UIColor whiteColor]];
}


-(void)loginidfocus:(id)sender{
    _login_name.textColor = darkcolor;
    [_loginid_line_center setBackgroundColor:darkcolor];
    [_loginid_line_left setBackgroundColor:darkcolor];
    [_loginid_line_right setBackgroundColor:darkcolor];
    _login_psw.textColor = [UIColor grayColor];
    [_loginpsw_line_center setBackgroundColor:lightcolor];
    [_loginpsw_line_left setBackgroundColor:lightcolor];
    [_loginpsw_line_right setBackgroundColor:lightcolor];
    
}

-(void)loginpswfocus:(id)sender{
    _login_psw.textColor = darkcolor;
    [_loginpsw_line_center setBackgroundColor:darkcolor];
    [_loginpsw_line_left setBackgroundColor:darkcolor];
    [_loginpsw_line_right setBackgroundColor:darkcolor];
    _login_name.textColor = [UIColor grayColor];
    [_loginid_line_center setBackgroundColor:lightcolor];
    [_loginid_line_left setBackgroundColor:lightcolor];
    [_loginid_line_right setBackgroundColor:lightcolor];
    
}

//初始化登录界面
-(void) initLoginview{
    
    _loginview.layer.cornerRadius = 0;
    _loginview.layer.masksToBounds = YES;
    //设置边框及边框颜色
    _loginview.layer.borderWidth = 2;
    _loginview.layer.borderColor =[ lightcolor CGColor];
    
    _login_name.textColor = darkcolor;
    _bt_login.backgroundColor = darkcolor;
    
    [_tf_psw setSecureTextEntry:YES];
    
    [_loginid_line_center setBackgroundColor:darkcolor];
    [_loginid_line_left setBackgroundColor:darkcolor];
    [_loginid_line_right setBackgroundColor:darkcolor];
    
    [_loginpsw_line_center setBackgroundColor:lightcolor];
    [_loginpsw_line_left setBackgroundColor:lightcolor];
    [_loginpsw_line_right setBackgroundColor:lightcolor];
}

#pragma mark 点击登录
-(void)login:(id)sender{
    _bt_login.backgroundColor = darkcolor;
    NSString *name = [_tf_name text];
    NSString *psw = [_tf_psw text];
    if(name==nil || [name isEqualToString:@""]){
        [self toastMessage:@"用户名不为空！"];
    }
    else if(psw==nil || [psw isEqualToString:@""]){
        [self toastMessage:@"密码不为空！"];
    }else{
        [self tologin:name :psw];
    }
    
    
}

#pragma mark 请求登录
- (void)tologin:(NSString *)name :(NSString *)psw
{
    [self showMBPHUD];
    NSMutableDictionary *param = [[NSMutableDictionary alloc]init];
    [param setObject:name forKey:@"loginid"];
    [param setObject:psw forKey:@"password"];
    [self startTask:@"Task_LoginCheck" withParams:param withTaskId:loginTaskId];
    
}

-(void)refreshUIWithThreadGuid:(int)taskId andData:(NSObject *)obj{
    [self hideMBPHUD];
    switch (taskId) {
        case loginTaskId:
            [self dealLoginData:(NSString*)obj];
            break;
            
        default:
            break;
    }
    
}

-(void)dealLoginData:(NSString *)bs{
    NSMutableArray *bd = (NSMutableArray *)bs;
    if ([[bd objectAtIndex:0]isEqualToString:@"1"])
    {
        NSString *xml = [bd objectAtIndex:2] ;
        if([xml rangeOfString:@"True"].location!=NSNotFound){
            NSString *name = [_tf_name text];

            [DBFrameService setConfigValue:WMH_User_Email    value:[EpointStringUtil getXMLFormatedAttribute:xml :@"email"]];
            [DBFrameService setConfigValue:WMH_User_TrueName value:[EpointStringUtil getXMLFormatedAttribute:xml :@"name"]];
            [DBFrameService setConfigValue:WMH_User_Phone    value:[EpointStringUtil getXMLFormatedAttribute:xml :@"phone"]];
            
            [DBFrameService setConfigValue:WMH_IsLogin value:@"1"];
            [DBFrameService setConfigValue:WMH_UserInfo value:xml];
            [DBFrameService setConfigValue:Epoint_User_LoginId value:name];
            //goto用户详情页面
            WMH_User_Detail_ViewController *view = [[WMH_User_Detail_ViewController alloc]init];
            [self.navigationController pushViewController:view animated:YES];
        }else{
             [self toastMessage:[bd objectAtIndex:1]];
        }
        
    }else{
        [self toastMessage:[bd objectAtIndex:1]];
    }
    
    
}




-(void)loginfocus:(id)sender{
    _bt_login.backgroundColor = darkcolor_light;
}

// 隐藏键盘
-(void)textFiledReturnEditing:(id)sender{
    [sender resignFirstResponder];
}

#pragma mark 用户注册
-(void)reg:(id)sender{
    WMH_User_Register_ViewController *reg = [[WMH_User_Register_ViewController alloc]init];
    [self.navigationController pushViewController:reg animated:YES];
}

#pragma mark 忘记密码
-(IBAction)resetpassword:(id)sender{
    //    WMH_ResetPasswordView *view = [[WMH_ResetPasswordView alloc]init];
    //    [self.navigationController pushViewController:view animated:YES];
}
@end
